//
//  ExploreCategroyViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/30.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "ExploreCategroyViewController.h"
#import "CataBottomCell.h"
#import "CataHeaderCell.h"
#import "CataMiddleCell.h"
@interface ExploreCategroyViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@end

@implementation ExploreCategroyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"秘探类型";
    [self createCollectionUI];
    // Do any additional setup after loading the view.
}

#pragma mark -创建collectionView
-(void)createCollectionUI{
    self.collectionView.scrollsToTop = YES;
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[CataHeaderCell class] forCellWithReuseIdentifier:@"CataHeaderCell"];
    [self.collectionView registerClass:[CataMiddleCell class] forCellWithReuseIdentifier:@"CataMiddleCell"];
    [self.collectionView registerClass:[CataBottomCell class] forCellWithReuseIdentifier:@"CataBottomCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cataHeader"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cataMiddle"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cataBottom"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
#pragma mark - collectionView的代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 10;
    }else{
        return 10;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CataHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CataHeaderCell" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 1){
        CataMiddleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CataMiddleCell" forIndexPath:indexPath];
        return cell;
    }else{
        CataBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CataBottomCell" forIndexPath:indexPath];
        return cell;
    }
}
//设置item的偏移量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return UIEdgeInsetsMake(5, 8, 5, 8);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return scaleTopAndBottom(1);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 0 || section == 2) {
        return 0;
    }else{
        return scaleLeftAndRight(5);
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(WIDTH, scaleTopAndBottom(410));
    }else if (indexPath.section == 1){
        return CGSizeMake(scaleLeftAndRight(175), scaleTopAndBottom(240));
    }else{
        return CGSizeMake(WIDTH - 20, scaleTopAndBottom(320));
    }
}
#pragma mark - 注册collectionView的头部视图

//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            return nil;
        }else if (indexPath.section == 1){
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cataMiddle" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            view.backgroundColor = [UIColor whiteColor];
            [self addHeaderViewContent:view AndChineseTitle:@"特别推荐" AndEnglishTitle:@"Specila recommeddatation"];
            return view;
        }else{
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cataBottom" forIndexPath:indexPath];
            for (UIView *subView in view.subviews) {
                [subView removeFromSuperview];
            }
            [self addHeaderViewContent:view AndChineseTitle:@"最新推荐" AndEnglishTitle:@"The latest recommended"];
            view.backgroundColor = [UIColor whiteColor];
            return view;
        }
    }else{
        return nil;
    }
}
//设置头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}

#pragma mark -头部视图
-(void)addHeaderViewContent:(UIView *)titleView AndChineseTitle:(NSString *)chineTile AndEnglishTitle:(NSString *)engTitle{
    UILabel *label = [[UILabel alloc] init];
    label.text = chineTile;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView.mas_centerX);
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.width.mas_equalTo(scaleLeftAndRight(80));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView.mas_left).offset(scaleLeftAndRight(80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.right.equalTo(label.mas_left).offset(scaleLeftAndRight(-5));
    }];
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleView.mas_right).offset(scaleLeftAndRight(-80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.left.equalTo(label.mas_right).offset(scaleLeftAndRight(5));
    }];
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = engTitle;
    bottomLabel.adjustsFontSizeToFitWidth = YES;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(titleView.mas_left);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
